package example;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.pi4.locutil.GeoPosition;

public class ResultParser {
	private File file;

	public ResultParser(File file) {
		if (file == null)
			throw new IllegalArgumentException("file cannot be null");
		this.file = file;
	}

	public ArrayList<Double> parse() throws NumberFormatException, IOException {
				
		ArrayList<Double> list = new ArrayList<Double>();
		
		BufferedReader in = new BufferedReader(new FileReader(file));
		String line;
		
		GeoPosition CorrectPosition = null;
		GeoPosition EstimatedPosition = null;
		
		try {
			// Process each line.
			// expected format :
			//(28.12, 7.57, 0.0);(-22.0, -10.75, 0.0)
			while ((line = in.readLine()) != null) {
				if (line.startsWith("#") || line.length() == 0)
					continue;
				
				String[] components = line.split(";");
				if(components.length == 2){
				    components[0] =	components[0].substring(1, components[0].length()-1);
				    components[1] =	components[1].substring(1, components[1].length()-1);
				    
					CorrectPosition = GeoPosition.parse(components[0]);
					EstimatedPosition = GeoPosition.parse(components[1]);
					list.add(CorrectPosition.distance(EstimatedPosition));
				}
			}
		}
		finally{
			in.close();
		}
		return list;
	}
}
